package com.kidedu.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.kidedu.dao.mapper.KNewsMapper;
import com.kidedu.entity.KNews;
import com.kidedu.entity.KNewsExample;
import com.kidedu.service.IKNewsService;

public class KNewsService implements IKNewsService{

    @Resource
    KNewsMapper kNewsMapper;
    
    @Override
    public List<KNews> selectKNewsByNstate(byte state, int count) {
    	List<KNews> kNewsList = new ArrayList<KNews>();
    	KNewsExample example = new KNewsExample();
    	example.createCriteria().andNstateEqualTo(state);
    	example.setOrderByClause("NcreatTime desc");
    	List<KNews> list = kNewsMapper.selectByExample(example);
    	int c = count > list.size() ? count : list.size();
    	for(int i = 0; i < c; i++){
    		kNewsList.add(list.get(i));
    	}
    	return kNewsList;
    }
	
	/**
	 * 插入kNews
	 * @param kNews
	 * @return
	 */
	public int insertKNews(KNews kNews) {
		KNewsExample example = new KNewsExample();
		example.setOrderByClause("Nid desc");
		List<KNews> kNewsList = kNewsMapper.selectByExample(example);
		if(kNewsList.size() > 0){
			kNews.setNid((byte)(kNewsList.get(0).getNid() + 1));
		}else{
			kNews.setNid((byte)1);
		}
		kNews.setNcreattime(new Date());
		return kNewsMapper.insert(kNews);
	}
}
